package com.sainan114.pipelinestudy.util;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;

/**
 * @author lgb
 * @Date: 2021/7/10
 */

public class ExpressUtils {
    private final static ExpressRunner runner = new ExpressRunner();

    public static String executeExpression(String ChineseScale, String mathScale, String strategyQlContent) {
        DefaultContext<String, Object> context = new DefaultContext<>();
        context.put("语文", ChineseScale);
        context.put("数学", mathScale);
        if (StringUtils.isBlank(strategyQlContent)) {
            LogUtils.infoLog("context is empty | judge failure ");
            return "预测失败";
        }
        Object result = null;
        try {
            result = runner.execute(strategyQlContent, context, null, true, false);
        } catch (Exception e) {
            LogUtils.errorLog("execute failure ChineseScale : " + ChineseScale + "mathScale:" + mathScale + "qlContent:" + strategyQlContent );
        }
        if (result instanceof String) {
            return String.valueOf(result);
        } else {
            return null;
        }
    }

    public static void main(String[] args) {

        LogUtils.infoLog("sss");
        HashMap<String, String> map = new HashMap<>();
        map.put("a", "a");
        map.put("b", "b");
        LogUtils.errorLog(map);
        String context = "if (语文 >= 95 && 数学 >= 95) {return '小卢考的还行'} else if (语文 >= 95 && 数学 < 95) { return '多做数学题'}" +
                "else if (语文 < 95 && 数学 >= 95) {return '多做语文题'} else {return '今晚学到2点'}";
        System.out.println(executeExpression("99", "98", context));
        System.out.println(executeExpression("95","88", context));
        System.out.println(executeExpression("90", "98", context));
        System.out.println(executeExpression("80", "88", context));

    }
}
